package view.order;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;
import main.WinkelApplication;
import model.Order;
import view.Manage;

/**
 * @author Dennis Nijssen
 */
public class OrderPanel extends JPanel {
    private JTable orderTable;
    private DefaultTableModel model;
    
    public OrderPanel() {
        super();
        
        setLayout(new BorderLayout());
        
        JLabel titel = new JLabel("Order beheren", SwingConstants.CENTER);
        titel.setFont(WinkelApplication.computeFontSize(2));
        add(titel, BorderLayout.PAGE_START);
        
        model = new DefaultTableModel();
        add(getScrollPaneWithTable(), BorderLayout.CENTER);
        
        add(getButtonBar(), BorderLayout.PAGE_END);
    }
    
    private void deleteOrder(int order_id) {
        WinkelApplication.getQueryManager().removeOrder(order_id);
        model.removeRow(orderTable.getSelectedRow());
    }
    
    private void editOrder(int order_id) {
        JFrame frame = new OrderEditFrame(order_id);
        frame.setSize(800, 400);
        frame.setLocation(
                (WinkelApplication.FRAME_WIDTH - frame.getWidth()) / 2, 
                (WinkelApplication.FRAME_HEIGHT - frame.getHeight()) / 2
            );
        
        frame.setResizable(false);
        frame.setVisible(true);

        frame.validate();
        frame.repaint();
    }
    
    private JPanel getButtonBar() {
        JPanel pnlButtonBar = new JPanel();
        pnlButtonBar.setPreferredSize(new Dimension(WinkelApplication.FRAME_WIDTH, 60));
        
        JButton btnEdit = new JButton("Wijzigen");
        btnEdit.setPreferredSize(new Dimension(150, 50));
        btnEdit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row;
                if((row = orderTable.getSelectedRow()) == -1) 
                    return;
                
                int order_id = new Integer(orderTable.getValueAt(row, 0).toString());
                editOrder(order_id);
            }
        });
        pnlButtonBar.add(btnEdit);
        
        JButton btnDelete = new JButton("Verwijderen");
        btnDelete.setPreferredSize(new Dimension(150, 50));
        btnDelete.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int row;
                if((row = orderTable.getSelectedRow()) == -1) 
                    return;
                
                int order_id = Integer.parseInt(orderTable.getValueAt(row, 0).toString());  
                int optionDialog = JOptionPane.showConfirmDialog(null, "Weet je zeker dat je deze order wilt verwijderen?", 
                        "Verwijder order", JOptionPane.YES_NO_OPTION);
                if(optionDialog == JOptionPane.YES_OPTION) 
                    deleteOrder(order_id);
            }
        });
        pnlButtonBar.add(btnDelete); 
        
        JButton btnBack = new JButton("Terug");
        btnBack.setPreferredSize(new Dimension(150, 50));
        btnBack.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                WinkelApplication instance = WinkelApplication.getInstance();
                instance.showPanel(new Manage(instance.getAccesLevel()));
            }
        });
        pnlButtonBar.add(btnBack);
        
        return pnlButtonBar;
    }
    
    private JScrollPane getScrollPaneWithTable() {
        model.addColumn("Order ID");
        model.addColumn("Datum");
        model.addColumn("Naam");
        model.addColumn("Adres");
        model.addColumn("Postcode");
        model.addColumn("Woonplaats");
        
        // Get all orders
        List<Order> orders = WinkelApplication.getQueryManager().getOrders();
        
        for(Order o : orders) {
            model.addRow(new Object[] {
                o.getOrderId(), o.getDatum(), 
                o.getNaam(), o.getAdres(), 
                o.getPostcode(), o.getWoonplaats() 
            });
        }
        
        // Create the table
        orderTable = new JTable(model) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        
        JScrollPane scrollPane = new JScrollPane(orderTable);
        return scrollPane;
    }
}
